<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (!($news_id = $route->getAdminId()))
	goToPage("/admin/news");

$errors = array();

if ($route->POST("action") == 'edit')
{
	$company = UV::readInt($route->POST("company"));
	$title = UV::readMQ($route->POST("title"));
	$link = translitIt(strip_tags($title)).".html";
	$i = 1;
	while ($db->getCell("select `link` from `news` where `link` = '$link' and `news_id` != '$news_id' limit 1")){
		$link = translitIt(strip_tags($title))."-".($i++).".html";
	}
	$preview = str_replace("\n","<br />",UV::readMQ($route->POST("preview")));
	$text = str_replace("\n","<br />",UV::readMQ($route->POST("text")));
	$date = strtotime(UV::readMQ($route->POST("date")));
	
	if (count($errors) == 0)
	{
		$query = "update `news` set `company_id` = '$company',`title`='$title',`link`='$link',`preview`='$preview',`text`='$text',`date`='$date' where `news_id` = '$news_id' limit 1";
		$update = $db->sendQuery($query);
		if (!$update)
			$errors[] = "Ошибка обновления записи";
	}
}

$news = $db->getRow("select * from `news` where `news_id` = '$news_id' limit 1");
if (!$news)
	goToPage("/admin/news");

loadPage("head.php");
?>
<style type='text/css'>
#ui-datepicker-div { font-size: 11pt; }
#ui-datepicker-div td { padding: 1px; }
</style>
<h1>Редактирование новости</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST("action") == 'edit')
		echo "<p class='success'>Успешно отредактировано<br /><br /></p>";
?>
<form method='POST' id="edit">
	<input type='hidden' name='action' value='edit' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='company'>Компания</label></td>
			<td width='600px'>
				<select name='company' id='company'><?php
					$companies = $db->getTable("select `company_id`,`name` from `companies` where `head_office` = '0' order by `name`");
					echo select_create($companies,"company_id","name",$news['company_id']);
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='title'>Заголовок</label></td>
			<td><input type='text' name='title' id='title' value="<?php echo htmlspecialchars($news['title']); ?>" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type='button' id='add_link' value="Добавить ссылку" style="width:150px;height:30px" /></td>
		</tr>
		<tr>
			<td><label for='preview'>Превью</label></td>
			<td><textarea name='preview' id='preview' style="height: 80px;"><?php echo str_replace("<br />","\n",$news['preview']); ?></textarea></td>
		</tr>
		<tr>
			<td><label for='text'>Текст</label></td>
			<td><textarea name='text' id='text'><?php echo str_replace("<br />","\n",$news['text']); ?></textarea></td>
		</tr>
		<tr>
			<td><label for='date'>Дата</label></td>
			<td><input class='short date' type='text' name='date' id='date' value='<?php echo date("d.m.Y",$news['date']); ?>' /></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Редактировать' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>